//
//  DispcoverViewController.swift
//  beegin2
//
//  Created by zsy on 16/3/18.
//  Copyright © 2016年 kszz. All rights reserved.
//

import UIKit

class DispcoverViewController: UIViewController,UICollectionViewDataSource ,UIScrollViewDelegate{
    
    let elements:[(name:String,parts:String)] = [
        ("维生素A","视觉|皮肤|骨骼"),
        ("维生素B6","免疫力|血管|吸收"),
        ("维生素D","钙磷吸收|牙齿|骨骼"),
        ("维生素B1","神经|消化|心血管"),
        ("维生素B12","发育|代谢|造血"),
        ("维生素E","神经|免疫"),
        ("维生素B2","新陈代谢|皮肤"),
        ("维生素C","牙齿|皮肤|免疫力"),
        ("维生素K","凝血"),
        
        ("烟酸(B3)","视觉|皮肤|骨骼"),
        ("叶酸","免疫力|血管|吸收"),
        ("钾","钙磷吸收|牙齿|骨骼"),
        ("生物素(H)","神经|消化|心血管"),
        ("钙","发育|代谢|造血"),
        ("镁","神经|免疫"),
        ("泛酸","新陈代谢|皮肤"),
        ("磷","牙齿|皮肤|免疫力"),
        ("钠","凝血"),
        
        ("氟","视觉|皮肤|骨骼"),
        ("碘","免疫力|血管|吸收"),
        ("钼","钙磷吸收|牙齿|骨骼"),
        ("铁","神经|消化|心血管"),
        ("硒","发育|代谢|造血"),
        ("锰","神经|免疫"),
        ("锌","新陈代谢|皮肤"),
        ("铜","牙齿|皮肤|免疫力"),
        ("铬","凝血")
    ]
    
    let symptoms:[(name:String,parts:String)] = [
        ("汗多夜啼","缺乏钙"),
        ("易出血","缺乏维生素K"),
        ("牙龈出血","缺乏维生素C"),
        ("皮肤干燥","缺乏维生素A"),
        ("口角溃疡","缺乏维生素B2"),
        ("舌苔厚","缺乏维生素C"),
        ("眼睛干涩","缺乏维生素E"),
        ("低血糖","缺乏泛酸"),
        ("食欲下降","缺铁/磷"),
        
        ("克汀病","缺乏碘"),
        ("湿疹","缺乏亚油酸"),
        ("腹胀厌食","缺乏钾"),
        ("抵抗力差","缺乏硒"),
        ("发育迟缓","缺乏蛋白质、铜、牛磺酸"),
        ("皮肤炎症","缺乏维生素B2"),
        ("龋齿","缺乏氟"),
        ("惊厥症","缺乏钙、镁"),
        ("皮肤弹性差","缺维生素A、E"),
        
        ("白殿风","缺乏L-酪氨酸"),
        ("佝偻病","缺乏维生素D"),
        ("",""),
        ("干眼症","缺乏β-胡萝卜素"),
        ("",""),
        ("",""),
        ("无力易昏倒","缺钠"),
        ("",""),
        ("","")
    ]


    @IBOutlet weak var typeOfSegment: UISegmentedControl!
    @IBOutlet weak var pageControl: UIPageControl!
    @IBOutlet weak var dipCollect: UICollectionView!
    @IBOutlet weak var searchBackView: UIView!
    @IBOutlet weak var declareLabel: UILabel!
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        typeOfSegment.setTitleTextAttributes([NSForegroundColorAttributeName : UIColor(red: 74 / 255, green: 74 / 255, blue: 74 / 255, alpha: 1)], forState: .Normal)
        typeOfSegment.setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.whiteColor()], forState: .Selected)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    @IBAction func segmentTypeChanged(sender: AnyObject) {
        
        if typeOfSegment.selectedSegmentIndex == 0{
        
            typeOfSegment.tintColor = UIColor(red: 255 / 255, green: 206 / 255, blue: 84 / 255, alpha: 100)
            
        }else{
        
            typeOfSegment.tintColor = UIColor(red: 237 / 255, green: 85 / 255, blue: 101 / 255, alpha: 100)
        }
        declareLabel.hidden = typeOfSegment.selectedSegmentIndex == 0 ? true : false
        searchBackView.backgroundColor = typeOfSegment.tintColor
        dipCollect.reloadData()
    }

    // MARK: UICollectionViewDataSource
    
     func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        //#warning Incomplete method implementation -- Return the number of sections
        return 3
    }
    
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        //#warning Incomplete method implementation -- Return the number of items in the section
        return 9
    }
    
    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAtIndex section: Int) -> CGFloat {
        return CGFloat(0)
    }
    
    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
        
        return CGSize(width: (collectionView.frame.width) / 3, height: collectionView.frame.height / 3)
    }
    
    func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        
        let index = Int(fabs(scrollView.contentOffset.x) / scrollView.frame.size.width)
        pageControl.currentPage = index
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
     
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("elementCell", forIndexPath: indexPath) as! ElementsCollectionViewCell
        cell.layer.borderWidth = 0.5
        
        cell.layer.borderColor = UIColor(red: 192 / 255, green: 192 / 255, blue: 192 / 255, alpha: 0.8).CGColor
        
        if typeOfSegment.selectedSegmentIndex == 0{
            cell.nameString = elements[indexPath.section * 9  + indexPath.row ].name
            cell.partsString = elements[indexPath.section * 9  + indexPath.row ].parts
        }else{
        
            cell.nameString = symptoms[indexPath.section * 9  + indexPath.row ].name
            cell.partsString = symptoms[indexPath.section * 9  + indexPath.row ].parts
        }

         cell.layoutIfNeeded()
        
        return cell
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
